package com.cskaoyan.service.wx_topic;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cskaoyan.bean.bo.wx_topic.WxTopicListBO;
import com.cskaoyan.bean.po.goods.AdminGoodsListPO;
import com.cskaoyan.bean.po.wx_topic.WxTopicDetailGoodsPO;
import com.cskaoyan.bean.po.wx_topic.WxTopicDetailTopicPO;
import com.cskaoyan.bean.po.wx_topic.WxTopicListPO;
import com.cskaoyan.bean.vo.goods.AdminGoodsListVO;
import com.cskaoyan.bean.vo.wx_topic.*;
import com.cskaoyan.mapper.wx_topic.WxTopicDetailGoodsMapper;
import com.cskaoyan.mapper.wx_topic.WxTopicDetailTopicMapper;
import com.cskaoyan.mapper.wx_topic.WxTopicListMapper;
import com.cskaoyan.util.beanutils.BeanUtil;
import com.cskaoyan.util.common.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.Data;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * @Author: 冯子靖
 * @Description:
 * @Date: 2022/7/21 10:53
 */
@Data
@Service
public class WxTopicServiceImpl implements WxTopicService {

    private Integer limit;

    private Integer page;

    private Integer pages;

    private Integer total;

    private List<WxTopicListPO> list;

    @Autowired
    WxTopicListMapper wxTopicListMapper;

    @Autowired
    WxTopicDetailGoodsMapper wxTopicDetailGoodsMapper;

    @Autowired
    WxTopicDetailTopicMapper wxTopicDetailTopicMapper;

    /**
     * topic页面
     *
     * @param wxTopicListBO
     * @return
     */
    @Override
    public WxTopicListVO printTopicList(WxTopicListBO wxTopicListBO) {

        page = wxTopicListBO.getPage();
        limit = wxTopicListBO.getLimit();

        PageHelper.startPage(page, limit);

        QueryWrapper<WxTopicListPO> WxTopicListWrapper = new QueryWrapper<>();
        WxTopicListWrapper.eq("deleted", 0);
        WxTopicListWrapper.orderByDesc("update_time");

        list = wxTopicListMapper.selectList(WxTopicListWrapper);

        PageInfo<WxTopicListPO> wxTopicListPOPageInfo = new PageInfo<>(list);
        total = (int) wxTopicListPOPageInfo.getTotal();
        pages = wxTopicListPOPageInfo.getPages();

        WxTopicListVO wxTopicListVO = new WxTopicListVO();

        try {
            BeanUtils.copyProperties(wxTopicListVO, this);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return wxTopicListVO;

    }

    /**
     * topic详情
     *
     * @param id
     * @return
     */
    @Override
    public WxTopicDetailVO printTopicDetail(Integer id) {
        WxTopicDetailTopicPO detailTopic = wxTopicDetailTopicMapper.selectById(id);
        String goods = detailTopic.getGoods();
        //用goodsId找goods
        Integer[] goodsIds = StringUtils.splitJsonToIntArrays(goods);
        List<Integer> goodsIdList = new ArrayList<>(goodsIds.length);
        Collections.addAll(goodsIdList, goodsIds);

        ArrayList<WxTopicDetailGoodsVO> wxTopicDetailGoodsVOS = new ArrayList<>();
        WxTopicDetailTopicVO wxTopicDetailTopicVO = new WxTopicDetailTopicVO();
        try {
            BeanUtils.copyProperties(wxTopicDetailTopicVO, detailTopic);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

        wxTopicDetailTopicVO.setGoods(goodsIds);
        if (goodsIdList.size() == 0) {

        } else {
            QueryWrapper<WxTopicDetailGoodsPO> detailGoodsWrapper = new QueryWrapper<>();

            detailGoodsWrapper.eq("deleted", 0);

            detailGoodsWrapper.in("id", goodsIdList);
            List<WxTopicDetailGoodsPO> goodsList = wxTopicDetailGoodsMapper.selectList(detailGoodsWrapper);

            try {
                for (WxTopicDetailGoodsPO wxTopicDetailGoodsPO : goodsList) {
                    WxTopicDetailGoodsVO wxTopicDetailGoodsVO = new WxTopicDetailGoodsVO();
                    BeanUtils.copyProperties(wxTopicDetailGoodsVO, wxTopicDetailGoodsPO);
                    wxTopicDetailGoodsVOS.add(wxTopicDetailGoodsVO);
                }
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        }

        return new WxTopicDetailVO(wxTopicDetailGoodsVOS, wxTopicDetailTopicVO);

    }

    /**
     * topic详情的topic推荐相关topic
     *
     * @param id
     * @return
     */
    @Override
    public WxTopicRelatedVO printTopicRelated(Integer id) {
        WxTopicDetailTopicPO currentTopic = wxTopicDetailTopicMapper.selectById(id);
        String goods = currentTopic.getGoods();
        //分割goodsIds
        Integer[] GoodsIdArr = StringUtils.splitJsonToIntArrays(goods);

        QueryWrapper<WxTopicDetailTopicPO> otherTopicWrapper = new QueryWrapper<>();

        List<WxTopicDetailTopicPO> wxTopicDetailTopicPOS = new ArrayList<>();
        otherTopicWrapper.orderByDesc("update_time");
        if (GoodsIdArr.length == 0) {
            otherTopicWrapper.last("limit 32");
            wxTopicDetailTopicPOS = wxTopicDetailTopicMapper.selectList(otherTopicWrapper);
        } else {
            if (GoodsIdArr.length == 1) {
                otherTopicWrapper.like("goods", GoodsIdArr[0]);
            } else {
                otherTopicWrapper.like("goods", GoodsIdArr[0]).or().like("goods", GoodsIdArr[1]);
            }
            wxTopicDetailTopicPOS = wxTopicDetailTopicMapper.selectList(otherTopicWrapper);
        }


        List<WxTopicDetailTopicVO> wxTopicDetailTopicVOS = new ArrayList<>();
        for (int i = 0; i < wxTopicDetailTopicPOS.size(); i++) {
            if (i == 4) {
                break;
            }
            WxTopicDetailTopicVO wxTopicDetailTopicVO = new WxTopicDetailTopicVO();
            try {
                BeanUtils.copyProperties(wxTopicDetailTopicVO, wxTopicDetailTopicPOS.get(i));
                Integer[] integers = StringUtils.splitJsonToIntArrays(wxTopicDetailTopicPOS.get(i).getGoods());
                wxTopicDetailTopicVO.setGoods(integers);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
            wxTopicDetailTopicVOS.add(wxTopicDetailTopicVO);

        }

        this.page = 1;
        this.limit = 4;
        this.total = wxTopicDetailTopicPOS.size();
        if (this.total % this.limit == 0) {
            this.pages = this.total / this.limit;
        } else {
            this.pages = this.total / this.limit + 1;
        }

        return new WxTopicRelatedVO(limit, page, pages, total, wxTopicDetailTopicVOS);

    }

    /**
     * topic详情的comment的list
     */
}
